package com.bailiban.aigou.admin.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.bailiban.aigou.admin.dao.CommodityclassificationDao;
import com.bailiban.aigou.admin.entity.BackstageUser;
import com.bailiban.aigou.admin.entity.Classification;
import com.bailiban.aigou.admin.utils.PageModel;
@Repository
public class CommodityclassificationDaoImpl implements CommodityclassificationDao {
	@Autowired
	private SessionFactory sessionFactory;
	
	public PageModel<Classification> getCommodityclassification(PageModel<Classification> pageModel) {
        Session session = sessionFactory.getCurrentSession();
		
		Criteria criteria = session.createCriteria(Classification.class);
		criteria.setFirstResult((pageModel.getPageNo()-1)*pageModel.getPageSize());
		criteria.setMaxResults(pageModel.getPageSize());
		
		List<Classification> list = criteria.list();
		pageModel.setList(list);
		return pageModel;
	}

	public int getCommodityclassificationCount() {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("select count(id) from Classification");
		List<Long> list = query.list();
		return list.get(0).intValue();
	}
	/**
	 *  添加商品分类
	 */
	public void save(Classification user) {
		sessionFactory.getCurrentSession().save(user);
		
	}
	/**
	 * 根据id获取商品分类
	 * @param id
	 * @return
	 * dao层实现层
	 */
	@Override
	public Classification getProductTypeById(String id) {
		Session session = sessionFactory.getCurrentSession();
		Classification user = session.get(Classification.class, id);
		return user;
	}
	/**
	 * 修改商品分类
	 * @param productType
	 *  dao层实现层
	 */
	@Override
	public void updateProductType(Classification classification) {
		Session session = sessionFactory.getCurrentSession();
		session.update(classification);
		
	}
	/**
	 * 删除商品分类
	 * @param productType
	 *  dao层实现层
	 */
	@Override
	public void deleteProductTypeById(String id) {
		Session session = sessionFactory.getCurrentSession();
		Classification classification=getProductTypeById(id);
		session.delete(classification);
		
	}
	/**
	 * 查询所有商品分类
	 * @return
	 * @throws SQLException
	 */
	@Override
	public List<Classification> getAllProductTypes() {
		Session session = sessionFactory.getCurrentSession();
		List<Classification> list = new ArrayList<Classification>();
		Query query = session.createQuery("from Classification");
		
		list=query.list();
		return list;
	}



	



	


}
